package cn.com.lms.business.report.web;

import cn.com.lms.business.core.constant.BNZConstants;
import cn.com.lms.business.course.service.FbCourseInfoService;
import cn.com.lms.business.exam.utils.BeanListUtils;
import cn.com.lms.business.questionnaire.persistence.entity.QuestionnaireSub;
import cn.com.lms.business.questionnaire.service.QuestionnairSubService;
import cn.com.lms.business.questionnaire.service.UserQuestionnaireService;
import cn.com.lms.business.report.service.CourseLearnReportService;
import cn.com.lms.business.report.service.CourseLearnUserReportService;
import cn.com.lms.business.report.web.vo.*;
import com.bnzj.core.persistence.query.CompareExpression;
import com.bnzj.core.persistence.query.CompareType;
import com.bnzj.core.persistence.query.IExpression;

import com.bnzj.core.persistence.query.QueryHelpBean;
import com.bnzj.core.rest.ResponseResult;
import com.bnzj.core.webmvc.util.WebMvcUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.hibernate.internal.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

/**
 * @author zhangzhenjiang
 * @createDate 2020/3/16
 * @description
 */
@Tag(name = "课程学习报表", description = "课程学习报表")
@RestController
@RequestMapping("/report")
public class CourseLearnReportController {

    @Autowired
    CourseLearnReportService courseLearnReportService;
    @Autowired
    CourseLearnUserReportService courseLearnUserReportService;

    @Autowired
    UserQuestionnaireService userQuestionnaireService;
    @Autowired
    QuestionnairSubService questionnairSubService;

    @Autowired
    FbCourseInfoService courseInfoService;
    @Operation(summary = "分页查询", description = "",
            parameters = {@Parameter(name = "keyWord", description = "关键子查询，课程名称进行模糊查询", allowEmptyValue = true,
                    allowReserved = true, schema = @Schema(implementation = String.class, description = "keyWord")),
                    @Parameter(name = "courseType", description = "课程类型", allowEmptyValue = true,
                            allowReserved = true, schema = @Schema(implementation = Short.class, description = "courseType")),
                    @Parameter(name = "startTime", description = "开始日期", allowEmptyValue = true,
                            allowReserved = true, schema = @Schema(implementation = Date.class, description = "startTime")),
                    @Parameter(name = "endTime", description = "日期结束", allowEmptyValue = true,
                            allowReserved = true, schema = @Schema(implementation = Date.class, description = "endTime")),
                    @Parameter(name = "limit", description = "分页大小", allowEmptyValue = true, allowReserved = true, schema = @Schema(type = "int", description = "分页大小")),
                    @Parameter(name = "offset", description = "页码", allowEmptyValue = true, allowReserved = true, schema = @Schema(type = "int", description = "页码")),
            })
    @GetMapping(value = "/course/learn/page")
    public ResponseResult<Page<CourseLearnReportVo>> getCourseLearnReportVoPage(HttpServletRequest request) {
        String startTime = request.getParameter("startTIme");
        String endTime = request.getParameter("endTime");
        String keyWord = request.getParameter("keyWord");
        String courseType = request.getParameter("courseType");
        Pageable pageable = WebMvcUtils.initPage(request, Sort.by(Sort.Direction.DESC, "c.create_time"));
        QueryHelpBean queryHelpBean = courseLearnReportService.getQueryHelpBean();
        queryHelpBean.setPageable(pageable);
        //日期查询
        if (startTime != null && endTime != null) {
            IExpression andCondition1 = CompareExpression.build("c.create_time", CompareType.greaterThanOrEqualTo, startTime);
            IExpression andCondition2 = CompareExpression.build("c.create_time", CompareType.lessThanOrEqualTo, endTime);
            queryHelpBean.addWhere(andCondition1.and(andCondition2));
        }
        if (StringHelper.isNotEmpty(keyWord)) {
            IExpression andCondition3 = CompareExpression.build("c.title", CompareType.like, keyWord);
            queryHelpBean.addWhere(andCondition3);
        }
        if (courseType != null) {
            IExpression andCondition4 = CompareExpression.build("c.course_type", CompareType.equal, courseType);
            queryHelpBean.addWhere(andCondition4);
        }
        Page<CourseLearnReportVo> all = courseLearnReportService.findAll(queryHelpBean);
        if(all.getTotalElements()>0) {
            all.stream().forEach(courseLearnReportVo -> {
                Map<String,Object> map=  userQuestionnaireService.getTotalQuestionnaireSubjectAvgScoreByCourseId(courseLearnReportVo.getId());
                if(map.get("score")==null){
                    courseLearnReportVo.setScore("未评分");
                }else{
                    courseLearnReportVo.setScore(String.format("%.2f", map.get("score")));
                }
            });
        }

        return ResponseResult.success(all);
    }


    @Operation(summary = "课程学习用户进度分页", description = "",
            parameters = {@Parameter(name = "keyWord", description = "关键子查询，课程名称进行模糊查询", allowEmptyValue = true,
                    allowReserved = true, schema = @Schema(implementation = String.class, description = "keyWord")),
                    @Parameter(name = "id", description = "课程ID", allowEmptyValue = false,
                            allowReserved = true, schema = @Schema(implementation = Long.class, description = "id")),
                    @Parameter(name = "limit", description = "分页大小", allowEmptyValue = true, allowReserved = true, schema = @Schema(type = "int", description = "分页大小")),
                    @Parameter(name = "offset", description = "页码", allowEmptyValue = true, allowReserved = true, schema = @Schema(type = "int", description = "页码")),

            })
    @GetMapping(value = "/course/learn/user/page")
    public ResponseResult<Page<CourseLearnUserReportVO>> getCourseLearnUserReportVOPage(HttpServletRequest request, @RequestParam(value = "keyWord", required = false) String keyWord, @RequestParam(value = "courseId", required = true) Long courseId) {

        Pageable pageable = WebMvcUtils.initPage(request, Sort.by(Sort.Direction.DESC, "s.last_study_time"));
        QueryHelpBean queryHelpBean = courseLearnUserReportService.getQueryHelpBean();
        queryHelpBean.setPageable(pageable);
        if (StringHelper.isNotEmpty(keyWord)) {
            IExpression andCondition3 = CompareExpression.build("s.user_name", CompareType.like, keyWord);
            queryHelpBean.addWhere(andCondition3);
        }
        if (courseId != null) {
            IExpression andCondition3 = CompareExpression.build("sd.obj_id", CompareType.equal, courseId);
            queryHelpBean.addWhere(andCondition3);
        }
        Page<CourseLearnUserReportVO> all = courseLearnUserReportService.findAll(queryHelpBean);
        return ResponseResult.success(all);
    }


    @Operation(summary = "课程问卷评估报告", description = "",
            parameters = {
                    @Parameter(name = "courseId", description = "课程ID", allowEmptyValue = false,
                            allowReserved = true, schema = @Schema(implementation = Long.class, description = "courseId")),
            })
    @GetMapping(value = "/course/learn/user/questionnaire")
    public ResponseResult<CourseQuestionnaireReportVO> getCourseQuestionnaireReportVO(HttpServletRequest request, @RequestParam(value = "courseId", required = true) Long courseId) {

        CourseQuestionnaireReportVO courseQuestionnaireReportVO = new CourseQuestionnaireReportVO();
        List<Map<String, Object>> mapList = userQuestionnaireService.getQuestionnaireSubjectAvgScoreByCourseId(courseId);
        List<QuestionnaireSubjectScoreVO> questionnaireSubjectScoreVOList = BeanListUtils.mapListToObjectList(mapList, QuestionnaireSubjectScoreVO.class);
        List<String> firtPartSubjectName = Arrays.asList("您对本次培训的整体评价", "课程时间的安排");
        List<String> secondPartSubjectName = Arrays.asList("培训的教材讲义", "课程结构、规划与进行方式", "课程是否达到预期目标满足您的期望", "课程内容是否理论与实践相结合", "课程对您的工作是否有帮助", "课程的学习资料是否易于理解和学习");
        List<String> thirdPartSubjectName = Arrays.asList("讲师对该课程的准备是否充分", "能清楚地表达和解说课程内容吗", "对所讲的课程具有足够的专业性吗", "讲师和学员的沟通与互动充分吗", "讲师对课程整体时间的掌握");
        List<String> allSubjectName = new ArrayList<>();
        allSubjectName.addAll(firtPartSubjectName);
        allSubjectName.addAll(secondPartSubjectName);
        allSubjectName.addAll(thirdPartSubjectName);

        List<QuestionnaireSubjectScoreVO> firstPartScoreList = new CopyOnWriteArrayList<>();
        List<QuestionnaireSubjectScoreVO> secondPartScoreList = new CopyOnWriteArrayList<>();
        List<QuestionnaireSubjectScoreVO> thirdPartScoreList = new CopyOnWriteArrayList<>();
        String questionnaireName = "", firstPartName = "整体安排", secondPartName = "课程部分", thirdPartName = "讲师部分";
        if (!CollectionUtils.isEmpty(questionnaireSubjectScoreVOList)) {
            firstPartScoreList = questionnaireSubjectScoreVOList.stream().filter(m -> firtPartSubjectName.contains((m.getQuestionnaireTop().replace("?","").replaceAll("？", "").trim()))).collect(Collectors.toList());
            secondPartScoreList = questionnaireSubjectScoreVOList.stream().filter(m -> secondPartSubjectName.contains((m.getQuestionnaireTop().replace("?","").replaceAll("？", "").trim()))).collect(Collectors.toList());
            thirdPartScoreList = questionnaireSubjectScoreVOList.stream().filter(m -> thirdPartSubjectName.contains((m.getQuestionnaireTop().replace("?","").replaceAll("？", "").trim()))).collect(Collectors.toList());
        }
        List<Number> firstPartAvgScoreList = new ArrayList<>();
        List<Number> secondPartAvgScoreList = new ArrayList<>();
        List<Number> thirdPartAvgScoreList = new ArrayList<>();
        Double firstDouble = 0D, secondDouble = 0D, thirdDouble = 0D, totalAvg = 0D;
        Double firstTotalDouble = 0D, secondTotalDouble = 0D, thirdTotalDouble = 0D,singleTotalScore=0D;
        if (!CollectionUtils.isEmpty(firstPartScoreList)) {
            firstPartScoreList.stream().forEach(firstPartScore -> {
                firstPartAvgScoreList.add(firstPartScore.getAvgScore());
            });
            firstDouble = firstPartAvgScoreList.stream().collect(Collectors.summingDouble(Number::doubleValue));
            firstPartName = "整体安排";
            questionnaireName = firstPartScoreList.get(0).getQuestionnaireName();
        }

        if (!CollectionUtils.isEmpty(secondPartScoreList)) {
            secondPartScoreList.stream().forEach(secondPartScore -> {
                secondPartAvgScoreList.add(secondPartScore.getAvgScore());
            });
            secondDouble = secondPartAvgScoreList.stream().collect(Collectors.summingDouble(Number::doubleValue));
            secondPartName = "课程部分";
            questionnaireName = secondPartScoreList.get(0).getQuestionnaireName();
        }


        if (!CollectionUtils.isEmpty(thirdPartScoreList)) {
            thirdPartScoreList.stream().forEach(thirdPartScore -> {
                thirdPartAvgScoreList.add(thirdPartScore.getAvgScore());
            });
            thirdDouble = thirdPartAvgScoreList.stream().collect(Collectors.summingDouble(Number::doubleValue));
            thirdPartName = "讲师部分";
            questionnaireName = thirdPartScoreList.get(0).getQuestionnaireName();
        }

        totalAvg = ((firstDouble + secondDouble + thirdDouble)/13)*20;
        singleTotalScore = firstDouble + secondDouble + thirdDouble;
        firstDouble=firstPartScoreList!=null &&firstPartScoreList.size()>0?(firstDouble/firstPartScoreList.size()):0;
        secondDouble=secondPartScoreList!=null &&secondPartScoreList.size()>0?(secondDouble/secondPartScoreList.size()):0;
        thirdDouble=thirdPartScoreList!=null &&thirdPartScoreList.size()>0?(thirdDouble/thirdPartScoreList.size()):0;

        Integer subjctCount=firstPartScoreList.size()+secondPartScoreList.size()+thirdPartScoreList.size();

        courseQuestionnaireReportVO.setFirstPartScoreList(firstPartScoreList);
        courseQuestionnaireReportVO.setSecondPartScoreList(secondPartScoreList);
        courseQuestionnaireReportVO.setThirdPartScoreList(thirdPartScoreList);
        courseQuestionnaireReportVO.setFirstPartAvgScore(Double.valueOf(String.format("%.2f", firstDouble)));
        courseQuestionnaireReportVO.setSecondPartAvgScore(Double.valueOf(String.format("%.2f", secondDouble)));
        courseQuestionnaireReportVO.setThirdPartAvgScore(Double.valueOf(String.format("%.2f", thirdDouble)));
        courseQuestionnaireReportVO.setTotalAvgScore(Double.valueOf(String.format("%.2f", totalAvg)));
        courseQuestionnaireReportVO.setQuestionnaireName(questionnaireName);
        courseQuestionnaireReportVO.setFirstPartName(firstPartName);
        courseQuestionnaireReportVO.setSecondPartName(secondPartName);
        courseQuestionnaireReportVO.setThirdPartName(thirdPartName);
        courseQuestionnaireReportVO.setSingleTotalAvgScore(subjctCount!=0?Double.valueOf(String.format("%.2f", singleTotalScore/subjctCount)):0);

        List<QuestionnaireSub>  questionnaireSubList=questionnairSubService.findAll();
        if (!CollectionUtils.isEmpty(questionnaireSubList)) {
            List<QuestionnaireSub>   firstQuestionnaireSubList = questionnaireSubList.stream().filter(m -> firtPartSubjectName.contains(m.getQuestionnaireTop().replace("?", "").replaceAll("？", "").trim())).collect(Collectors.toList());
            List<QuestionnaireSub>   secondQuestionnaireSubList = questionnaireSubList.stream().filter(m -> secondPartSubjectName.contains(m.getQuestionnaireTop().replace("?", "").replaceAll("？", "").trim())).collect(Collectors.toList());
            List<QuestionnaireSub>   thirdQuestionnaireSubList = questionnaireSubList.stream().filter(m -> thirdPartSubjectName.contains(m.getQuestionnaireTop().replace("?", "").replaceAll("？", "").trim())).collect(Collectors.toList());


            if(!CollectionUtils.isEmpty(firstQuestionnaireSubList)){
                if(CollectionUtils.isEmpty(firstPartScoreList)){
                    List<QuestionnaireSubjectScoreVO> firstPartScoreDefaultList = new CopyOnWriteArrayList<>();
                    firstQuestionnaireSubList.stream().forEach(questionnaireSub -> {
                        QuestionnaireSubjectScoreVO questionnaireSubjectScoreVO=new QuestionnaireSubjectScoreVO();
                        questionnaireSubjectScoreVO.setAvgScore(0);
                        questionnaireSubjectScoreVO.setQuestionnaireInfoId(questionnaireSub.getQuestionnaireInfoId());
                        questionnaireSubjectScoreVO.setQuestionnaireTop(questionnaireSub.getQuestionnaireTop());
                        questionnaireSubjectScoreVO.setQuestionnaireSubId(questionnaireSub.getId());
                        questionnaireSubjectScoreVO.setQuestionnaireName("");
                        firstPartScoreDefaultList.add(questionnaireSubjectScoreVO);

                    });

                    courseQuestionnaireReportVO.setFirstPartScoreList(firstPartScoreDefaultList);

                }
                if(CollectionUtils.isEmpty(secondPartScoreList)) {
                    List<QuestionnaireSubjectScoreVO> secondPartScoreDefaultList = new CopyOnWriteArrayList<>();
                    secondQuestionnaireSubList.stream().forEach(questionnaireSub -> {
                        QuestionnaireSubjectScoreVO questionnaireSubjectScoreVO = new QuestionnaireSubjectScoreVO();
                        questionnaireSubjectScoreVO.setAvgScore(0);
                        questionnaireSubjectScoreVO.setQuestionnaireInfoId(questionnaireSub.getQuestionnaireInfoId());
                        questionnaireSubjectScoreVO.setQuestionnaireTop(questionnaireSub.getQuestionnaireTop());
                        questionnaireSubjectScoreVO.setQuestionnaireSubId(questionnaireSub.getId());
                        questionnaireSubjectScoreVO.setQuestionnaireName("");
                        secondPartScoreDefaultList.add(questionnaireSubjectScoreVO);

                    });
                    courseQuestionnaireReportVO.setSecondPartScoreList(secondPartScoreDefaultList);
                }

                if(CollectionUtils.isEmpty(thirdPartScoreList)) {
                    List<QuestionnaireSubjectScoreVO> thirdPartScoreDefaultList = new CopyOnWriteArrayList<>();
                    thirdQuestionnaireSubList.stream().forEach(questionnaireSub -> {
                        QuestionnaireSubjectScoreVO questionnaireSubjectScoreVO=new QuestionnaireSubjectScoreVO();
                        questionnaireSubjectScoreVO.setAvgScore(0);
                        questionnaireSubjectScoreVO.setQuestionnaireInfoId(questionnaireSub.getQuestionnaireInfoId());
                        questionnaireSubjectScoreVO.setQuestionnaireTop(questionnaireSub.getQuestionnaireTop());
                        questionnaireSubjectScoreVO.setQuestionnaireSubId(questionnaireSub.getId());
                        questionnaireSubjectScoreVO.setQuestionnaireName("");
                        thirdPartScoreDefaultList.add(questionnaireSubjectScoreVO);
                    });
                    courseQuestionnaireReportVO.setThirdPartScoreList(thirdPartScoreDefaultList);
                }


            }

        }



        return ResponseResult.success(courseQuestionnaireReportVO);
    }



    @Operation(summary = "导出课程评估报告", description = "导出课程评估报告")
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public void downloadTemplate(HttpServletResponse response,@RequestBody CourseIdVO courseIdVO) {
        Long courseId=courseIdVO.getCourseId();
        String password = "";
        if (courseIdVO.getIsReadOnly()) {
            //如果是导出只读的，需从数据字典读取配置好的密码
            password = courseInfoService.getSysSettingValue(BNZConstants.EXCEL_PASSWORD);
        }
        CourseQuestionnaireReportVO courseQuestionnaireReportVO = new CourseQuestionnaireReportVO();
        List<Map<String, Object>> mapList = userQuestionnaireService.getQuestionnaireSubjectAvgScoreByCourseId(courseId);
        List<QuestionnaireSubjectScoreVO> questionnaireSubjectScoreVOList = BeanListUtils.mapListToObjectList(mapList, QuestionnaireSubjectScoreVO.class);
        List<String> firtPartSubjectName = Arrays.asList("您对本次培训的整体评价", "课程时间的安排");
        List<String> secondPartSubjectName = Arrays.asList("培训的教材讲义", "课程结构、规划与进行方式", "课程是否达到预期目标满足您的期望", "课程内容是否理论与实践相结合", "课程对您的工作是否有帮助", "课程的学习资料是否易于理解和学习");
        List<String> thirdPartSubjectName = Arrays.asList("讲师对该课程的准备是否充分", "能清楚地表达和解说课程内容吗", "对所讲的课程具有足够的专业性吗", "讲师和学员的沟通与互动充分吗", "讲师对课程整体时间的掌握");
        List<QuestionnaireSubjectScoreVO> firstPartScoreList = new CopyOnWriteArrayList<>();
        List<QuestionnaireSubjectScoreVO> secondPartScoreList = new CopyOnWriteArrayList<>();
        List<QuestionnaireSubjectScoreVO> thirdPartScoreList = new CopyOnWriteArrayList<>();
        String questionnaireName = courseIdVO.getCourseName(), firstPartName = "整体安排", secondPartName = "课程部分", thirdPartName = "讲师部分";
        if (!CollectionUtils.isEmpty(questionnaireSubjectScoreVOList)) {
            firstPartScoreList = questionnaireSubjectScoreVOList.stream().filter(m -> firtPartSubjectName.contains(m.getQuestionnaireTop().replace("?", "").replaceAll("？", ""))).collect(Collectors.toList());
            secondPartScoreList = questionnaireSubjectScoreVOList.stream().filter(m -> secondPartSubjectName.contains(m.getQuestionnaireTop().replace("?", "").replaceAll("？", ""))).collect(Collectors.toList());
            thirdPartScoreList = questionnaireSubjectScoreVOList.stream().filter(m -> thirdPartSubjectName.contains(m.getQuestionnaireTop().replace("?", "").replaceAll("？", ""))).collect(Collectors.toList());
        }
        List<Number> firstPartAvgScoreList = new ArrayList<>();
        List<Number> secondPartAvgScoreList = new ArrayList<>();
        List<Number> thirdPartAvgScoreList = new ArrayList<>();
        Double firstDouble = 0D, secondDouble = 0D, thirdDouble = 0D, totalAvg = 0D;
        Double firstTotalDouble = 0D, secondTotalDouble = 0D, thirdTotalDouble = 0D,singleTotalScore=0D;
        if (!CollectionUtils.isEmpty(firstPartScoreList)) {
            firstPartScoreList.stream().forEach(firstPartScore -> {
                firstPartAvgScoreList.add(firstPartScore.getAvgScore());
            });
            firstDouble = firstPartAvgScoreList.stream().collect(Collectors.summingDouble(Number::doubleValue))/firstPartScoreList.size();
            firstTotalDouble = firstPartAvgScoreList.stream().collect(Collectors.summingDouble(Number::doubleValue));
            firstPartName = "整体安排";
            questionnaireName = courseIdVO.getCourseName()+"-"+firstPartScoreList.get(0).getQuestionnaireName();
        }

        if (!CollectionUtils.isEmpty(secondPartScoreList)) {
            secondPartScoreList.stream().forEach(secondPartScore -> {
                secondPartAvgScoreList.add(secondPartScore.getAvgScore());
            });
            secondDouble = secondPartAvgScoreList.stream().collect(Collectors.summingDouble(Number::doubleValue))/secondPartScoreList.size();
            secondTotalDouble = secondPartAvgScoreList.stream().collect(Collectors.summingDouble(Number::doubleValue));
            secondPartName = "课程部分";
            questionnaireName =  courseIdVO.getCourseName()+"-"+secondPartScoreList.get(0).getQuestionnaireName();
        }


        if (!CollectionUtils.isEmpty(thirdPartScoreList)) {
            thirdPartScoreList.stream().forEach(thirdPartScore -> {
                thirdPartAvgScoreList.add(thirdPartScore.getAvgScore());
            });
            thirdDouble = thirdPartAvgScoreList.stream().collect(Collectors.summingDouble(Number::doubleValue))/thirdPartScoreList.size();
            thirdTotalDouble = thirdPartAvgScoreList.stream().collect(Collectors.summingDouble(Number::doubleValue));
            thirdPartName = "讲师部分";
            questionnaireName =  courseIdVO.getCourseName()+"-"+thirdPartScoreList.get(0).getQuestionnaireName();
        }

        totalAvg = ((firstTotalDouble + secondTotalDouble + thirdTotalDouble)/13)*20;
        singleTotalScore=firstTotalDouble + secondTotalDouble + thirdTotalDouble;


        courseQuestionnaireReportVO.setFirstPartScoreList(firstPartScoreList);
        courseQuestionnaireReportVO.setSecondPartScoreList(secondPartScoreList);
        courseQuestionnaireReportVO.setThirdPartScoreList(thirdPartScoreList);
        courseQuestionnaireReportVO.setFirstPartAvgScore(Double.valueOf(String.format("%.2f", firstDouble)));
        courseQuestionnaireReportVO.setSecondPartAvgScore(Double.valueOf(String.format("%.2f", secondDouble)));
        courseQuestionnaireReportVO.setThirdPartAvgScore(Double.valueOf(String.format("%.2f", thirdDouble)));
        courseQuestionnaireReportVO.setTotalAvgScore(totalAvg);
        courseQuestionnaireReportVO.setQuestionnaireName(questionnaireName);
        courseQuestionnaireReportVO.setFirstPartName(firstPartName);
        courseQuestionnaireReportVO.setSecondPartName(secondPartName);
        courseQuestionnaireReportVO.setThirdPartName(thirdPartName);
        courseQuestionnaireReportVO.setSingleTotalAvgScore(singleTotalScore);

        List<QuestionnaireSub>  questionnaireSubList=questionnairSubService.findAll();
        if (!CollectionUtils.isEmpty(questionnaireSubList)) {
            List<QuestionnaireSub>   firstQuestionnaireSubList = questionnaireSubList.stream().filter(m -> firtPartSubjectName.contains(m.getQuestionnaireTop().replace("?", "").replaceAll("？", "").trim())).collect(Collectors.toList());
            List<QuestionnaireSub>   secondQuestionnaireSubList = questionnaireSubList.stream().filter(m -> secondPartSubjectName.contains(m.getQuestionnaireTop().replace("?", "").replaceAll("？", "").trim())).collect(Collectors.toList());
            List<QuestionnaireSub>   thirdQuestionnaireSubList = questionnaireSubList.stream().filter(m -> thirdPartSubjectName.contains(m.getQuestionnaireTop().replace("?", "").replaceAll("？", "").trim())).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(firstQuestionnaireSubList)){
                if(CollectionUtils.isEmpty(firstPartScoreList)){
                    List<QuestionnaireSubjectScoreVO> firstPartScoreDefaultList = new CopyOnWriteArrayList<>();
                    firstQuestionnaireSubList.stream().forEach(questionnaireSub -> {
                        QuestionnaireSubjectScoreVO questionnaireSubjectScoreVO=new QuestionnaireSubjectScoreVO();
                        questionnaireSubjectScoreVO.setAvgScore(0);
                        questionnaireSubjectScoreVO.setQuestionnaireInfoId(questionnaireSub.getQuestionnaireInfoId());
                        questionnaireSubjectScoreVO.setQuestionnaireTop(questionnaireSub.getQuestionnaireTop());
                        questionnaireSubjectScoreVO.setQuestionnaireSubId(questionnaireSub.getId());
                        questionnaireSubjectScoreVO.setQuestionnaireName("");
                        firstPartScoreDefaultList.add(questionnaireSubjectScoreVO);

                    });

                    courseQuestionnaireReportVO.setFirstPartScoreList(firstPartScoreDefaultList);

                }
                if(CollectionUtils.isEmpty(secondPartScoreList)) {
                    List<QuestionnaireSubjectScoreVO> secondPartScoreDefaultList = new CopyOnWriteArrayList<>();
                    secondQuestionnaireSubList.stream().forEach(questionnaireSub -> {
                        QuestionnaireSubjectScoreVO questionnaireSubjectScoreVO = new QuestionnaireSubjectScoreVO();
                        questionnaireSubjectScoreVO.setAvgScore(0);
                        questionnaireSubjectScoreVO.setQuestionnaireInfoId(questionnaireSub.getQuestionnaireInfoId());
                        questionnaireSubjectScoreVO.setQuestionnaireTop(questionnaireSub.getQuestionnaireTop());
                        questionnaireSubjectScoreVO.setQuestionnaireSubId(questionnaireSub.getId());
                        questionnaireSubjectScoreVO.setQuestionnaireName("");
                        secondPartScoreDefaultList.add(questionnaireSubjectScoreVO);

                    });
                    courseQuestionnaireReportVO.setSecondPartScoreList(secondPartScoreDefaultList);
                }

                if(CollectionUtils.isEmpty(thirdPartScoreList)) {
                    List<QuestionnaireSubjectScoreVO> thirdPartScoreDefaultList = new CopyOnWriteArrayList<>();
                    thirdQuestionnaireSubList.stream().forEach(questionnaireSub -> {
                        QuestionnaireSubjectScoreVO questionnaireSubjectScoreVO=new QuestionnaireSubjectScoreVO();
                        questionnaireSubjectScoreVO.setAvgScore(0);
                        questionnaireSubjectScoreVO.setQuestionnaireInfoId(questionnaireSub.getQuestionnaireInfoId());
                        questionnaireSubjectScoreVO.setQuestionnaireTop(questionnaireSub.getQuestionnaireTop());
                        questionnaireSubjectScoreVO.setQuestionnaireSubId(questionnaireSub.getId());
                        questionnaireSubjectScoreVO.setQuestionnaireName("");
                        thirdPartScoreDefaultList.add(questionnaireSubjectScoreVO);
                    });
                    courseQuestionnaireReportVO.setThirdPartScoreList(thirdPartScoreDefaultList);
                }


            }

        }




        HSSFWorkbook hssfWorkbook = courseLearnReportService.exportCourseQuestionnaireReport(courseQuestionnaireReportVO,password);
        response.setContentType("application/octet-stream");
        //默认Excel名称
        response.setHeader("Content-Disposition", "attachment;fileName=template.xls");

        try {
            response.flushBuffer();
            hssfWorkbook.write(response.getOutputStream());
        } catch (IOException e) {
//            e.printStackTrace();
        }

    }





}
